package controller

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisRpc"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Json"
	"gitee.com/fierce_wolf/go-fox-edge-kernel-common/gatewayRouter"
	"strings"
)

type topicController struct {
}

func (e *topicController) Subscribe(message string) {
	restFulRequestVO := &commRedisRpc.RestFulRequestVO{}
	err := json.Unmarshal([]byte(message), &restFulRequestVO)
	if err != nil {
		return
	}

	root := "/gateway/route"

	// 添加路由
	if (root+"/add") == restFulRequestVO.Uri && "post" == restFulRequestVO.Method {
		jsn := Json.BuildJson(restFulRequestVO.Data)

		vo := &gatewayRouter.RouterVO{}
		err := json.Unmarshal([]byte(jsn), &vo)
		if err != nil {
			return
		}

		gatewayRouter.SetRouterConfig(vo)
	}
	// 更新路由
	if (root+"/update") == restFulRequestVO.Uri && "post" == restFulRequestVO.Method {
		jsn := Json.BuildJson(restFulRequestVO.Data)

		vo := &gatewayRouter.RouterVO{}
		err := json.Unmarshal([]byte(jsn), &vo)
		if err != nil {
			return
		}

		gatewayRouter.SetRouterConfig(vo)
	}

	// 更新路由
	if strings.HasPrefix(restFulRequestVO.Uri, root+"/delete/") && "delete" == restFulRequestVO.Method {
		id := restFulRequestVO.Uri[len(root+"/delete/"):]
		gatewayRouter.DelRouterConfig(id)
	}

}
